package com.appclient;

import com.carpark.ejb.Model.EmployeesModel;
import configurationReader.Config;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;

/**
 *
 * @author Sawior
 */
public class LoginController {

    public static Integer PERSON_LOGIN_ID;
    public static String PERSON_LOGIN_NAME;
    public static String PERSON_LOGIN_LASTNAME;
    public static String PERSON_LOGIN_JOBTYPE="";
    private static final String WRONG_PASS_OR_USER = "Zły użytkownik lub hasło";
    @FXML
    private TextField user;
    @FXML
    private PasswordField password;
    @FXML
    private Button close;
    @FXML
    private Button login;
    @FXML
    private Label error;

    public void actionHandler(ActionEvent event) {
        if (event.getSource() == close) {
            Start.stage.close();
        }
        if (event.getSource() == login) {
            if (isAuthorization()) {
                MenuController.firstLogin=true;
                Start.stage.setScene(Start.mainScene);
            }
            error.setText(WRONG_PASS_OR_USER);
        }
    }

    private boolean isAuthorization() {
        if (this.user.getText().equals("admin") && this.password.getText().equals(Config.INSTANCE.getAdminPassword())) {
            PERSON_LOGIN_NAME = "Pan";
            PERSON_LOGIN_LASTNAME = "Administrator";
            PERSON_LOGIN_JOBTYPE="admin";
            Start.mainPanelController.setLoginPersonNameAndLastName();
            return true;
        }
        List<EmployeesModel> dao = DaoInstanceEnum.INSTANCE.getDao().getAllEmployees();
        for (EmployeesModel empl : dao) {
            if (empl.getSurname() == null || empl.getPassword() == null) {
            } else {
                if (empl.getSurname().equals(this.user.getText()) && empl.getPassword().equals(codePassword(this.password.getText()))) {
                    PERSON_LOGIN_ID = empl.getEmployeeId();
                    PERSON_LOGIN_NAME = empl.getFirstname();
                    PERSON_LOGIN_LASTNAME = empl.getSurname();
                    PERSON_LOGIN_JOBTYPE = empl.getJobType();
                    Start.mainPanelController.setLoginPersonNameAndLastName();
                    
                    
                    return true;
                }
            }
        }
        return false;
    }

    private void setLoginPerson() {

    }

    private String codePassword(String password) {
        MessageDigest md = null;
        try {
            md = MessageDigest.getInstance("MD5");
        } catch (NoSuchAlgorithmException ex) {
            Logger.getLogger(LoginController.class.getName()).log(Level.SEVERE, "[CarPark] Critical error during login to main system.", ex);
        }
        md.update(password.getBytes());
        byte byteData[] = md.digest();
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < byteData.length; i++) {
            sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1));
        }

        return sb.toString();
    }
}
